#mapping-script
# -*- coding: ms949 -*-
import bots.transform as transform
import bots.usersys.mappings.utils as utils

def main(inn,out):
    #XML
    
    ns1 = '{urn:kr:gov:kcs:data:standard:KCS_AmendmentOfReleaseOrCarryInSchemaModule:1:0:0}'
    ns2 = '{urn:kr:gov:kcs:common:standard:KCS_ReusableAggregateBusinessInformationEntitySchemaModule:1:0:0}'
    rootelement = ns1+ 'AmendmentOfReleaseOrCarryIn'
    
    #Read from inn by EDIFACT order
    DocumentTypeCode = inn.get({'BOTSID':'UNH'},{'BOTSID':'BGM','C002.1001':None})
    PreviousCustomsDocumentReferenceID = inn.get({'BOTSID':'UNH'},{'BOTSID':'BGM','1004':None})
    ApplicationTypeCode = inn.get({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.1131':'5AI','C529.7365':None})
    DocumentIssueDate = inn.get({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'141','C507.2380':None})
    ApplicationGradeNumeric = inn.get({'BOTSID':'UNH'},{'BOTSID':'QTY','C186.6063':'5BB','C186.6060':None})
    
    AmendmentReason = inn.get({'BOTSID':'UNH'},{'BOTSID':'FTX','4451':'ACD','C108.4440#1':None})
    AmendmentReason2 = inn.get({'BOTSID':'UNH'},{'BOTSID':'FTX','4451':'ACD','C108.4440#2':None})
    if AmendmentReason2 != None: AmendmentReason = AmendmentReason + ' ' + AmendmentReason2
    
    ApplicationPartyName = inn.get({'BOTSID':'UNH'},{'BOTSID':'NAD','3035':'DT','C058.3124#1':None})
    
    UCRIdentificationID = inn.get({'BOTSID':'UNH'},{'BOTSID':'NAD'},{'BOTSID':'RFF','C506.1153':'XC','C506.1154':None})
    UCRMasterBLSequenceID = inn.get({'BOTSID':'UNH'},{'BOTSID':'NAD'},{'BOTSID':'RFF','C506.1153':'XC','C506.1156':None})
    UCRHouseBLSequenceID = inn.get({'BOTSID':'UNH'},{'BOTSID':'NAD'},{'BOTSID':'RFF','C506.1153':'XC','C506.4000':None})
    
    out.ta_info['botskey'] = 'CUSDMR-5LC-' + PreviousCustomsDocumentReferenceID.strip()
    
    #0~99 Loop
    for lin in inn.getloop({'BOTSID':'UNH'},{'BOTSID':'LIN'}):
        AmendmentContentLineNumberID = lin.get({'BOTSID':'LIN','C212.7140':None})
        AmendmentContentBeforeDescription = lin.get({'BOTSID':'LIN'},{'BOTSID':'FTX','4451':'AAO','C108.4440#1':None})
        AmendmentContentAfterDescription = lin.get({'BOTSID':'LIN'},{'BOTSID':'FTX','4451':'CHG','C108.4440#1':None})
        
        lou = out.putloop({'BOTSID':rootelement,'BOTSID':ns1+'AmendmentContent'})
        lou.put({'BOTSID':ns1+'AmendmentContent',ns2+'LineNumberID':AmendmentContentLineNumberID})
        lou.put({'BOTSID':ns1+'AmendmentContent',ns2+'BeforeDescription':AmendmentContentBeforeDescription})
        lou.put({'BOTSID':ns1+'AmendmentContent',ns2+'AfterDescription':AmendmentContentAfterDescription})
    #Loop End
    
          
    #Write to out by XML order
    out.put({'BOTSID':rootelement,ns1+'ApplicationTypeCode':ApplicationTypeCode})
    out.put({'BOTSID':rootelement,ns1+'ApplicationGradeNumeric':ApplicationGradeNumeric})
    out.put({'BOTSID':rootelement,ns1+'AmendmentReason':AmendmentReason})
    
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'TypeCode':DocumentTypeCode})
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'IssueDate':utils.isoDateFormat(DocumentIssueDate)})
    
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'PreviousCustomsDocument',ns2+'referenceID':PreviousCustomsDocumentReferenceID})
    
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'ApplicationParty',ns2+'Name':ApplicationPartyName})
    
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'UCR',ns2+'IdentificationID':UCRIdentificationID})
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'UCR',ns2+'MasterBLSequenceID':UCRMasterBLSequenceID})
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'UCR',ns2+'HouseBLSequenceID':UCRHouseBLSequenceID})
    
    out.put({'BOTSID':rootelement,ns1+'AmendmentOfReleaseOrCarryIn__{http://www.w3.org/2001/XMLSchema-instance}schemaLocation':'urn:kr:gov:kcs:data:standard:KCS_AmendmentOfReleaseOrCarryInSchemaModule:1:0:0 ./schema/uncefact/data/KCS_AmendmentOfReleaseOrCarryInSchemaModule_1.0.0_standard.xsd'})